package lib.bone.nepudata.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuSubjectDictDAO;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.nepudata.dao.NepuSubjectDAO;
import lib.bone.nepudata.domain.entity.NepuSubject;
import lib.bone.nepudata.expose.WorkloadService;
import lib.bone.nepudata.service.ResearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
/**
 * @Author : cg
 * @Date : 2022-1-13
 * 横向课题关联表
 */
/**
 * cg 2022-4-6
 */

/**
 * @tips: 程功未实现具体细节，具体内容由 @kindear 实现
 */
@Service
public class NepuSubjectServiceImpl implements ResearchService<NepuSubject,String> {
    @Autowired
    NepuSubjectDAO nepuSubjectDAO;

    @Autowired
    NepuSubjectDictDAO dictDAO;


    @Autowired(required = false)
    WorkloadService workloadService;

    @Override
    public String getProjectCode(String s) {
        Optional<NepuSubject> entityOp = getById(s);
        if (entityOp.isPresent()){
            return entityOp.get().getSubjectCode();
        }else {
            return "无";
        }
    }

    @Override
    public List<NepuSubject> getByIdIn(List<String> ids) {
        return nepuSubjectDAO.findByIdIn(ids);
    }

    public Optional<NepuSubject> findBySubjectNameAndLeader(String subjectName, String leader){
        return nepuSubjectDAO.findBySubjectNameAndLeader(subjectName,leader);
    }

    public List<NepuSubject> findByCode(String code){
        return nepuSubjectDAO.findBySubjectCode(code);
    }


    @Override
    public String getServiceType() {
        return ResearchTypeConst.SUBJECT;
    }

    @Override
    public NepuSubject saveOne(NepuSubject nepuSubject) {
        if (Validator.isNotEmpty(nepuSubject.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuSubjectDAO.save(nepuSubject);
    }

    @Override
    public NepuSubject updateOne(NepuSubject nepuSubject) {
        if (Validator.isEmpty(nepuSubject.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuSubjectDAO.save(nepuSubject);      }

    @Override
    public PageResultDTO<NepuSubject> getAll(Pageable pageable, Example<NepuSubject> example) {
        Page<NepuSubject> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuSubjectDAO.findAll(pageable);
        }else {
            page = nepuSubjectDAO.findAll(example,pageable);
        }
        List<NepuSubject> list = page.getContent();
        PageResultDTO<NepuSubject> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        System.out.println("进入横向课题删除！");
        Integer useCount = workloadService.countByLinkId(id);
        // Long useCount = nepuResearchWorkloadDAO.countByResearchTypeAndMapperId("教学科研获奖",id);
        if(useCount > 0){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该奖励的科研工作量信息,拒绝执行删除!");
        }
        //判断数据是否存在
        Optional<NepuSubject> nepuSubjectOp = getById(id);
        if(!nepuSubjectOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
        nepuSubjectDAO.deleteById(id);
        System.out.println("横向课题实体类删除成功！");
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuSubject> getById(String id) {
        return nepuSubjectDAO.findById(id);
    }

//    @Override
//    public ResponseDTO<NepuSubject> saveOne(NepuSubjectAddDTO dto) {
//        //@TODO 该部分可以用dictService 替换
//        NepuSubject nepuSubject = new NepuSubject();
//       Optional<NepuSubjectDict> dictOp = dictService.getSubjectDict(dto.getSubjectType(), dto.getFunds());
//        if (!dictOp.isPresent()){
//            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM,"没有满足条件的计算规则");
//        }
//        NepuSubjectDict dict = dictOp.get();
//        //初始化赋值为最大值
//        nepuSubject.setWorkload(dict.getWorkloadMax());
//        //词典信息写入
//        BeanUtil.copyProperties(dto,nepuSubject,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        nepuSubject.setSubjectDictId(dict.getId());
//        return ResponseDTO.succData(nepuSubjectDAO.save(nepuSubject));
//    }
//
//    @Override
//    public ResponseDTO<NepuSubject> updateOne(NepuSubjectUpdateDTO nepuSubjectUpdateDTO) {
//
//        //@TODO 如果在审核流程中 应拒绝修改，或者修改后退回初始流程位置
//        Optional<NepuSubject> nepuSubjectOp = getById(nepuSubjectUpdateDTO.getId());
//        if(!nepuSubjectOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+
//                nepuSubjectUpdateDTO.getId()+"]对应的教学科研奖励不存在");
//        NepuSubject nepuSubject = nepuSubjectOp.get();
//        BeanUtil.copyProperties(nepuSubjectUpdateDTO,nepuSubject,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        return ResponseDTO.succData(nepuSubjectDAO.save(nepuSubject));
//    }
}
